package services

import (
	"context"
	"fmt"
	"io"
	"log"
	"time"
)

type ProdService struct {
	//UnimplementedProdServiceServer
}

func (this *ProdService) GetProdStock(ctx context.Context, request *ProdRequest) (*ProdResponse, error) {
	var stock int32 = 0
	if request.ProdArea == ProdAreas_A {
		stock = 20
	} else if request.ProdArea == ProdAreas_B {
		stock = 30
	} else {
		stock = 60
	}
	return &ProdResponse{
		ProdStock: stock,
	}, nil
}

func (this *ProdService) GetProdStocks(ctx context.Context, size *QuerySize, ) (*ProdResponseList, error) {
	Proders := []*ProdResponse{
		&ProdResponse{ProdStock: 25},
		&ProdResponse{ProdStock: 26},
		&ProdResponse{ProdStock: 27},
		&ProdResponse{ProdStock: 28},
	}

	return &ProdResponseList{
		Prodres: Proders,
	}, nil
}

func (this *ProdService) GetProdInfo(ctx context.Context, in *ProdRequest) (*ProdModel, error) {
	ret := &ProdModel{
		ProdId:    20,
		ProdName:  "测试商品",
		ProdPrice: 20.5,
	}

	return ret, nil
}

// 流式接收客户端发送的商品信息，计算总价格
func (this *ProdService) GetProdGoods(stream ProdService_GetProdGoodsServer) error {
	var totalPrice float32
	startTime := time.Now() // 开始接收流时间
	for {
		good, err := stream.Recv()
		if err == io.EOF { // 客户端发送流结束
			endTime := time.Now() // 结束时间
			fmt.Println("startTime:", startTime, "endTime:", endTime)
			return stream.SendAndClose(&ProdResponseTotalPrice{
				TotalPrice: totalPrice,
			})
		}
		if err != nil {
			return err
		}
		fmt.Println(good)
		totalPrice += good.ProdPrice
		time.Sleep(100 * time.Millisecond)
	}
}

// 接收用户发送的区号，服务端流式发送当前区的商品返回
func (this *ProdService) GetProdAreasGoods(rect *ProdRequestArea, stream ProdService_GetProdAreasGoodsServer) error {
	// 拿到发送的值
	log.Println("获取客户端的值：", rect)
	var goods []*ProdModel
	if rect.ProdArea == ProdAreas_A {
		goods = []*ProdModel{
			&ProdModel{ProdId: 101, ProdName: "A区测试商品1号", ProdPrice: 10.2},
			&ProdModel{ProdId: 102, ProdName: "A区测试商品2号", ProdPrice: 10.2},
			&ProdModel{ProdId: 103, ProdName: "A区测试商品3号", ProdPrice: 10.2},
			&ProdModel{ProdId: 104, ProdName: "A区测试商品4号", ProdPrice: 10.2},
		}
	}else if rect.ProdArea == ProdAreas_B {
		goods = []*ProdModel{
			&ProdModel{ProdId: 101, ProdName: "B区测试商品1号", ProdPrice: 10.2},
			&ProdModel{ProdId: 102, ProdName: "B区测试商品2号", ProdPrice: 10.2},
			&ProdModel{ProdId: 103, ProdName: "B区测试商品3号", ProdPrice: 10.2},
			&ProdModel{ProdId: 104, ProdName: "B区测试商品4号", ProdPrice: 10.2},
		}
	}else{
		goods = []*ProdModel{
			&ProdModel{ProdId: 101, ProdName: "C区测试商品1号", ProdPrice: 10.2},
			&ProdModel{ProdId: 102, ProdName: "C区测试商品2号", ProdPrice: 10.2},
			&ProdModel{ProdId: 103, ProdName: "C区测试商品3号", ProdPrice: 10.2},
			&ProdModel{ProdId: 104, ProdName: "C区测试商品4号", ProdPrice: 10.2},
		}
	}

	for _, good := range goods {
		if err := stream.Send(good); err != nil {
			return err
		}
		time.Sleep(time.Second)
	}
	return nil
}

// 接收用户发送的区号(双向流)
func (this *ProdService) GetProdAreaListGoods(stream ProdService_GetProdAreaListGoodsServer) error{
	for {
		rect, err := stream.Recv()
		if err == io.EOF{
			break
		}
		if err != nil{
			return err
		}

		log.Println(rect.ProdArea)
		var goods []*ProdModel
		if rect.ProdArea == ProdAreas_A{
			goods = []*ProdModel{
				&ProdModel{ProdId: 101, ProdName: "A区测试商品1号", ProdPrice: 10.2},
				&ProdModel{ProdId: 102, ProdName: "A区测试商品2号", ProdPrice: 10.2},
				&ProdModel{ProdId: 103, ProdName: "A区测试商品3号", ProdPrice: 10.2},
				&ProdModel{ProdId: 104, ProdName: "A区测试商品4号", ProdPrice: 10.2},
			}
		}else if rect.ProdArea == ProdAreas_B {
			goods = []*ProdModel{
				&ProdModel{ProdId: 101, ProdName: "B区测试商品1号", ProdPrice: 10.2},
				&ProdModel{ProdId: 102, ProdName: "B区测试商品2号", ProdPrice: 10.2},
				&ProdModel{ProdId: 103, ProdName: "B区测试商品3号", ProdPrice: 10.2},
				&ProdModel{ProdId: 104, ProdName: "B区测试商品4号", ProdPrice: 10.2},
			}
		}else{
			goods = []*ProdModel{
				&ProdModel{ProdId: 101, ProdName: "C区测试商品1号", ProdPrice: 10.2},
				&ProdModel{ProdId: 102, ProdName: "C区测试商品2号", ProdPrice: 10.2},
				&ProdModel{ProdId: 103, ProdName: "C区测试商品3号", ProdPrice: 10.2},
				&ProdModel{ProdId: 104, ProdName: "C区测试商品4号", ProdPrice: 10.2},
			}
		}

		for _, good := range goods {
			if err := stream.Send(good); err != nil {
				return err
			}
			time.Sleep(time.Second)
		}
	}
	return nil
}
